﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_index : System.Web.UI.Page
{
    db_admin admin = new db_admin();
    db_teacher teacher = new db_teacher();
    string[] adminInfo;
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["adminName"] != null)
            {
                Server.Transfer("admin_index.aspx");
            }
        }
        catch { }
    }
    protected void ibtn_res_Click(object sender, ImageClickEventArgs e)
    {
        tb_adminName.Text = "";
        tb_code.Text = "";
        tb_pwd.Text = "";
    }
    protected void ibtn_login_Click(object sender, ImageClickEventArgs e)
    {
        string name = Tools.Clearstr(tb_adminName.Text,20);
        string pwd = Tools.Clearstr(tb_pwd.Text,20);
        string code =Tools.Clearstr(tb_code.Text,20);
        bool isErrorCode = true;
        try
        {

            if (code != Session["code"].ToString()) //判断验证码是否正确
            {
                isErrorCode = true;//如果不正确，则为true
            }
            else {
                isErrorCode = false;
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('验证码超时，请重新登录！');top.location.href='index.aspx';</script>");
        }

        if (isErrorCode)
        {
            Response.Write("<script language=javascript>alert('验证码错误！！！');</script>");
        }
        else
        {
            adminInfo = admin.isLogin(name, Tools.mymd5(pwd));
            if (adminInfo != null)
            {
                Session["adminName"] = name;
                Session["adminID"] = adminInfo[0];
                Session["lastTime"] = adminInfo[1];
                Session["lastIP"] = adminInfo[2];
                Server.Transfer("admin_index.aspx");
                //Response.Redirect("admin_index.aspx");
            }
            else
            {
                adminInfo = teacher.isLogin(name, Tools.mymd5(pwd));
                if (adminInfo != null)
                {
                    Session["teacherId"] = name;
                    Session["teacherName"] = adminInfo[0];
                    Session["lastTime"] = adminInfo[1];
                    Session["lastIP"] = adminInfo[2];
                    Server.Transfer("admin_index.aspx");
                }
                else
                {
                    Session["adminName"] = null;
                    Session["teacherId"] = null;
                    Session["adminID"] = null;
                    Session["lastTime"] = null;
                    Session["lastIP"] = null;
                    Response.Write("<script language=javascript>alert('用户名或密码错误，登陆不成功！！！');</script>");
                }
            }
        }
        
        
        
    }
}